<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px" class="top-left">
      <el-form-item label prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入人员姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dept:labPerson:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['dept:labPerson:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['dept:labPerson:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['dept:labPerson:export']">导出</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table border stripe class="table" v-loading="loading" :data="labPersonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="人员姓名" align="center" prop="userName" min-width="120" />
      <el-table-column label="邮箱" align="center" prop="email" show-overflow-tooltip min-width="120" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
      <el-table-column label="性别" align="center" prop="sex" width="100">
        <template slot-scope="scope">{{ scope.row.sex == 0 ? '女' : '男' }}</template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">{{ scope.row.status == 0 ? '停用' : '正常' }}</template>
      </el-table-column>

      <el-table-column label="参加工作时间" align="center" prop="joinWorkDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinWorkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="技术职称" align="center" prop="profess" show-overflow-tooltip min-width="120" />
      <el-table-column label="备注" align="center" prop="remark" min-width="120" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['report_lq:labPerson:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['report_lq:labPerson:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改试验室人员信息对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="人员姓名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入人员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入用户邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="form.sex" style="width: 100%;">
                <el-radio label="1">男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status" style="width: 100%;">
                <el-radio label="1">正常</el-radio>
                <el-radio label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参加工作时间" prop="joinWorkDate" class="narrow-spacing">
              <el-date-picker clearable v-model="form.joinWorkDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择参加工作时间" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="技术职称" prop="profess">
              <el-input v-model="form.profess" placeholder="请输入技术职称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="证书编号" prop="professCode">
              <el-input v-model="form.professCode" placeholder="请输入证书编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="职称获取时间" prop="professDate" class="narrow-spacing">
              <el-date-picker clearable v-model="form.professDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择职称获取时间" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="生日" label-width="110px" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listLabPerson, getLabPerson, delLabPerson, addLabPerson, updateLabPerson } from "@/api/dept/labPerson";
import {mapGetters} from "vuex";

export default {
  name: "LabPerson",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试验室人员信息表格数据
      labPersonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        userName: undefined,
        email: undefined,
        phonenumber: undefined,
        sex: undefined,
        status: undefined,
        joinWorkDate: undefined,
        birthday: undefined,
        profess: undefined,
        professCode: undefined,
        professDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // userName: [
        //   { required: true, message: "人员姓名不能为空", trigger: "blur" }
        // ],
        // email: [
        //   { required: true, message: "用户邮箱不能为空", trigger: "blur" }
        // ],
        // phonenumber: [
        //   { required: true, message: "手机号码不能为空", trigger: "blur" }
        // ],
        // sex: [
        //   { required: true, message: "用户性别不能为空", trigger: "change" }
        // ],
        // status: [
        //   { required: true, message: "状态不能为空", trigger: "change" }
        // ],
        // remark: [
        //   { required: true, message: "备注不能为空", trigger: "blur" }
        // ],
        // joinWorkDate: [
        //   { required: true, message: "参加工作时间不能为空", trigger: "blur" }
        // ],
        // birthday: [
        //   { required: true, message: "生日不能为空", trigger: "blur" }
        // ],
        // profess: [
        //   { required: true, message: "技术职称不能为空", trigger: "blur" }
        // ],
        // professCode: [
        //   { required: true, message: "证书编号不能为空", trigger: "blur" }
        // ],
        // professDate: [
        //   { required: true, message: "职称获取时间不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  watch: {
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.getList();
    }
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId", "userDeptId"])
  },
  created() {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getList();
  },
  methods: {
    /** 查询试验室人员信息列表 */
    getList() {
      this.loading = true;
      listLabPerson(this.queryParams).then((response) => {
        this.labPersonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        userName: undefined,
        email: undefined,
        phonenumber: undefined,
        sex: undefined,
        status: undefined,
        remark: undefined,
        joinWorkDate: undefined,
        birthday: undefined,
        profess: undefined,
        professCode: undefined,
        professDate: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加试验室人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getLabPerson(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改试验室人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateLabPerson(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addLabPerson(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除试验室人员信息编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delLabPerson(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "report_lq/labPerson/export",
        {
          ...this.queryParams
        },
        `labPerson_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep {
    .table {
      .el-table__fixed::before,
      .el-table__fixed-right::before {
        height: 0px !important;
      }
    }
  }
</style>
